using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace SmartContentDB.Interfaces
{
    public class User
    {
        #region Enum
        public enum ClientType
        {
            Administrator = 0,
            Regular = 1,
            QA = 2
        }
        #endregion

        #region Variables
        private int id;
        private string user;
        private string encrypted_pass;
        private string decrypted_pass;
        private string email;
        private ClientType clientType;
        private string fullName;
        #endregion

        #region Constructor
        public User()
        {
            this.id = -1;
            this.user = null;
            this.encrypted_pass = null;
            this.decrypted_pass = null;
            this.fullName = null;
            this.clientType = ClientType.Regular;
            this.email = null;
        }
        public User(int id, string user, string password, string email, string fullName, ClientType type, bool encrypted)
        {
            this.id = id;
            this.user = user;
            if (encrypted)
            {
                this.encrypted_pass = password;
                this.decrypted_pass = null;
            }
            else
            {
                this.encrypted_pass = null;
                this.decrypted_pass = password;
            }
            this.fullName = fullName;
            this.email = email;
            this.clientType = type;
        }
        #endregion

        #region Set/Get
        public int getId()
        {
            return id;
        }
        public string getUser()
        {
            return user;
        }
        public string getEncryptedPass()
        {
            if (this.encrypted_pass == null)
            {
                byte[] Results;
                System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
                MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
                byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes("SmartContentPhrase"));
                TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
                TDESAlgorithm.Key = TDESKey;
                TDESAlgorithm.Mode = CipherMode.ECB;
                TDESAlgorithm.Padding = PaddingMode.PKCS7;
                byte[] DataToEncrypt = UTF8.GetBytes(this.decrypted_pass);

                try
                {
                    ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                    Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
                }
                finally
                {
                    TDESAlgorithm.Clear();
                    HashProvider.Clear();
                }
                this.encrypted_pass = Convert.ToBase64String(Results);
            }
            return this.encrypted_pass;
        }
        public string getDecryptedPass()
        {
            if (this.decrypted_pass == null)
            {
                byte[] Results;
                System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
                MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
                byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes("SmartContentPhrase"));
                TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
                TDESAlgorithm.Key = TDESKey;
                TDESAlgorithm.Mode = CipherMode.ECB;
                TDESAlgorithm.Padding = PaddingMode.PKCS7;
                byte[] DataToDecrypt = Convert.FromBase64String(this.encrypted_pass);
                try
                {
                    ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                    Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
                }
                finally
                {
                    TDESAlgorithm.Clear();
                    HashProvider.Clear();
                }
                this.decrypted_pass = UTF8.GetString( Results );
            }
            return decrypted_pass;
        }
        public ClientType getClientType()
        {
            return this.clientType;
        }
        public string getEMail()
        {
            return this.email;
        }
        public string getName()
        {
            return this.fullName;
        }
        #endregion

        #region Overrides
        public override string ToString()
        {
            return this.user;
        }
        #endregion
    }
}
